package com.rt.easyjava.common.dozer.converter;

import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.StringUtils;
import org.dozer.DozerConverter;

import java.sql.Timestamp;

/**
 * Created by wxy on 2017/9/13.
 */
public class DateConverter extends DozerConverter<Timestamp,String> {


    public DateConverter() {
        super(Timestamp.class, String.class);
    }

    @Override
    public String convertTo(Timestamp source, String destination) {
        if(source == null)return null;
        String format = getParameter();
        String dateStr = DateTool.date2String(source, format);
        return dateStr;
    }

    @Override
    public Timestamp convertFrom(String source, Timestamp destination) {
        if(StringUtils.isNull(source))return null;
        String format = getParameter();
        Timestamp timestamp ;
        if(format.contains("23:59:59")){
            timestamp = DateTool.timestampAppend235959999(DateTool.str2Timestamp(source, "yyyy-MM-dd"));
        }else {
            timestamp = DateTool.str2Timestamp(source, format);
        }
        return timestamp;
    }
}
